function showMsg(obj, msg){
    $(obj).text(msg).fadeIn('fast', function(){
        setTimeout(function(){
            $(obj).fadeOut('fast');
        }, 3000);
    });
}

$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        sequentialUploads: true,
        send: function (e, data) {
            $('#fileupload-zone').hide();
            $('#fileupload-progress').fadeIn('fast');
            $('#list-thumbnails').fadeIn('fast');
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#fileupload-progress > div > div').css('width', progress + '%');
            if(progress > 90){
                $('#fileupload-progress').fadeOut('fast', function(){
                    $('#fileupload-zone').slideDown('fast');
                });
            }
        },
        done: function (e, data) {
            var listThumb = $('#list-thumbnails');
            $.each(data.result, function (index, file) {
                if(file.url != undefined) {
                    var row = $('<li><div class="thumbnail">' +
                        '<img alt="" src="'+ file.thumbnail_url +'">' +
                        '<div class="caption"><p>' + file.name + 
                        '<button class="close" data-type="' + file.delete_type + 
                        '" data-url="' + file.delete_url + 
                        '">&times;</button></p></div>' + 
                        '<input type="hidden" name="photoName[]" value="'+ file.name +'" />' + 
                        '<input type="hidden" name="photoUrl[]" value="'+ file.url +'" />' + 
                        '<input type="hidden" name="photoThumb[]" value="'+ file.thumbnail_url +'" /></div></li>');
                    listThumb.append(row);
                }
            });
        }
    });
    
     $('#fileupload').bind('fileuploadcompleted', function (e, data) {
         console.log(e, data);
     })
    
    //Create albums ajax
    $('#btnAlbumAdd').bind('click', function(){
        
        var albumName = $('#albumName').val();
    
        if(albumName == '' || albumName == undefined){
            showMsg('#albumErrorMsg', 'Bạn chưa điền tên album.');
            return false;
        }
        
        if($('input[name="photoName[]"]').length < 1){
            showMsg('#albumErrorMsg', 'Bạn phải upload ít nhất 1 ảnh.');
            return false;
        }
        
        //Call Ajax to create new category
        $.ajax({
            type: 'POST',
            url: config.base_url + 'backend/albums/create_ajax',
            data: $('#albumAddFrom').serialize() + '&' + 
                  $('input[name="photoName[]"]').serialize() + '&' + 
                  $('input[name="photoUrl[]"]').serialize() + '&' +
                  $('input[name="photoThumb[]"]').serialize(),
            dataType: 'json',
            beforeSend: function(){
                $('#albumWaitingMsg').fadeIn('fast');
            },
            success: function(response){
                if(response.error == 0){
                    document.location.href = config.base_url + 'backend/albums';
                }else{
                    showMsg('#cateErrorMsg', response.message);
                }
            },
            error: function(e){
                console.log(e);
            }
        });

        return false;
    });
});